2020-2021 Sunseeker Telemetry and Lighting System
eusci_a_spi_ex1_master.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 //*****************************************************************************
49 // MSP430FR5739
75 //*****************************************************************************
76 
77 uint8_t RXData =0;
78 uint8_t TXData = 0;
79 
80 void main(void)
81 {
82  volatile uint16_t i;
83 
84  //Stop watchdog timer
85  WDT_A_hold(WDT_A_BASE);
86 
87  //Set P1.0 as an output pin.
88  /*
89 
90  * Select Port 1
91  * Set Pin 0 as output
92  */
93  GPIO_setAsOutputPin(
94  GPIO_PORT_P1,
95  GPIO_PIN0
96  );
97  //Set P1.0 as Output Low.
98  /*
99 
100  * Select Port 1
101  * Set Pin 0 to output Low.
102  */
104  GPIO_PORT_P1,
105  GPIO_PIN0
106  );
107 
108  // Configure Pins for LFXIN
109  //Set PJ.4 and PJ.5 as Primary Module Function Input.
110  /*
111 
112  * Select Port J
113  * Set Pin 4, 5 to input Primary Module Function, (LFXIN).
114  */
115  GPIO_setAsPeripheralModuleFunctionInputPin(
116  GPIO_PORT_PJ,
117  GPIO_PIN4 + GPIO_PIN5,
118  GPIO_PRIMARY_MODULE_FUNCTION
119  );
120 
121  //Set external frequency for XT1
122  CS_setExternalClockSource(32768,0);
123  //Set DCO frequency to max DCO setting
124  CS_setDCOFreq(CS_DCORSEL_0,CS_DCOFSEL_3);
125  //Select XT1 as the clock source for ACLK with no frequency divider
126  CS_initClockSignal(CS_ACLK,CS_XT1CLK_SELECT,CS_CLOCK_DIVIDER_1);
127  //Start XT1 with no time out
128  CS_turnOnXT1(CS_XT1_DRIVE_0);
129 
130  // Configure SPI pins
131  // Configure Pins for UCA0CLK
132  //Set P1.5 as Secondary Module Function Input.
133  /*
134 
135  * Select Port 1
136  * Set Pin 5 to input Secondary Module Function, (UCA0CLK).
137  */
138  GPIO_setAsPeripheralModuleFunctionInputPin(
139  GPIO_PORT_P1,
140  GPIO_PIN5,
141  GPIO_SECONDARY_MODULE_FUNCTION
142  );
143  // Configure Pins for UCA0TXD/UCA0SIMO, UCA0RXD/UCA0SOMI
144  //Set P2.0, P2.1 as Secondary Module Function Input.
145  /*
146 
147  * Select Port 2
148  * Set Pin 0, 1 to input Secondary Module Function, (UCA0TXD/UCA0SIMO, UCA0RXD/UCA0SOMI).
149  */
150  GPIO_setAsPeripheralModuleFunctionInputPin(
151  GPIO_PORT_P2,
152  GPIO_PIN0 + GPIO_PIN1,
153  GPIO_SECONDARY_MODULE_FUNCTION
154  );
155 
156  //Initialize Master
157  EUSCI_A_SPI_initMasterParam param = {0};
158  param.selectClockSource = EUSCI_A_SPI_CLOCKSOURCE_ACLK;
159  param.clockSourceFrequency = CS_getACLK();
160  param.desiredSpiClock = 500000;
161  param.msbFirst = EUSCI_A_SPI_MSB_FIRST;
162  param.clockPhase = EUSCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
163  param.clockPolarity = EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
164  param.spiMode = EUSCI_A_SPI_3PIN;
165  EUSCI_A_SPI_initMaster(EUSCI_A0_BASE, &param);
166 
167  //Enable SPI module
168  EUSCI_A_SPI_enable(EUSCI_A0_BASE);
169 
170  EUSCI_A_SPI_clearInterrupt(EUSCI_A0_BASE,
171  EUSCI_A_SPI_RECEIVE_INTERRUPT);
172  // Enable USCI_A0 RX interrupt
173  EUSCI_A_SPI_enableInterrupt(EUSCI_A0_BASE,
174  EUSCI_A_SPI_RECEIVE_INTERRUPT);
175 
176  //Wait for slave to initialize
177  __delay_cycles(100);
178 
179  TXData = 0x1; // Holds TX data
180 
181  //USCI_A0 TX buffer ready?
182  while (!EUSCI_A_SPI_getInterruptStatus(EUSCI_A0_BASE,
183  EUSCI_A_SPI_TRANSMIT_INTERRUPT)) ;
184 
185  //Transmit Data to slave
186  EUSCI_A_SPI_transmitData(EUSCI_A0_BASE, TXData);
187 
188 
189  __bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts
190  __no_operation(); // Remain in LPM0
191 
192 }
193 
194 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
195 #pragma vector=USCI_A0_VECTOR
196 __interrupt
197 #elif defined(__GNUC__)
198 __attribute__((interrupt(USCI_A0_VECTOR)))
199 #endif
200 void USCI_A0_ISR (void)
201 {
202  switch (__even_in_range(UCA0IV,4)){
203  //Vector 2 - RXIFG
204  case 2:
205  //USCI_A0 TX buffer ready?
206  while (!EUSCI_A_SPI_getInterruptStatus(EUSCI_A0_BASE,
207  EUSCI_A_SPI_TRANSMIT_INTERRUPT)) ;
208 
209  RXData = EUSCI_A_SPI_receiveData(EUSCI_A0_BASE);
210 
211  //Increment data
212  TXData++;
213 
214  //Send next value
215  EUSCI_A_SPI_transmitData(EUSCI_A0_BASE,
216  TXData
217  );
218 
219  //Delay between transmissions for slave to process information
220  __delay_cycles(40);
221 
222  break;
223  default: break;
224  }
225 }
uint8_t RXData
void main(void)
void USCI_A0_ISR(void)
uint8_t TXData
MPU_initThreeSegmentsParam param
__no_operation()
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)